package com.api.gbhg.luoao.utils;

import cn.hutool.core.util.StrUtil;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.google.common.base.Joiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;

import java.util.*;

public class information {

    private static final Logger logger = LoggerFactory.getLogger(information.class);

    //查询科目代码
    public static String kmdm(String id){
        if (StrUtil.isNotEmpty(id)){

            RecordSet rs = new RecordSet();
            rs.executeQuery("select  kmdm  from uf_gxmdygnkm where id = "+id+"");
            rs.next();
            return rs.getStringNoTrim("kmdm");
        }

        return "";
    }

    //查询经办人
    public static String jbrname(String id) {
        if (StrUtil.isNotEmpty(id)) {

        List name = new ArrayList();
        RecordSet rs = new RecordSet();
        rs.executeQuery("select lastname from hrmResource where id in("+id+")");
        while (rs.next()){
            name.add(rs.getStringNoTrim("lastname"));
        }

        return Joiner.on(",").join((Iterable<?>) name);
    }
       return "";
    }
    //查询经费科室审核人
    public static String jfzgksshr(String id) {
        if (StrUtil.isNotEmpty(id)) {

            List name = new ArrayList();
            RecordSet rs = new RecordSet();
            rs.executeQuery("select lastname from hrmResource where id in("+id+")");
            while (rs.next()){
                name.add(rs.getStringNoTrim("lastname"));
            }

            return Joiner.on(",").join((Iterable<?>) name);
        }
        return "";
    }


    //查询部门
    public static  String bmname(String id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select departmentmark from HrmDepartment where id = "+id+"");
        rs.next();
        return rs.getStringNoTrim("departmentmark");
    }

    //查询单位
    public static  String dwname(String id){
        RecordSet rs = new RecordSet();
        String sql = "select SUBCOMPANYNAME from hrmsubcompany where id = "+id+"";
        //logger.info("id:"+id+"查询单位sql打印："+ sql);
        if(id.isEmpty()){
            return "";
        }
        rs.executeQuery(sql);
        rs.next();
        return rs.getStringNoTrim("SUBCOMPANYNAME");
    }


    //查询下拉框
    public static String getselectname(String requestId,String fieldname,String value){
        if (StrUtil.isNotEmpty(requestId)&&StrUtil.isNotEmpty(fieldname)&&StrUtil.isNotEmpty(value)) {
            RecordSet rs = new RecordSet();
            rs.executeQuery("select wf_si.selectvalue,wf_si.selectname from\n" +
                    "workflow_billfield wf_bf \n" +
                    ",workflow_selectitem wf_si, \n" +
                    "(select s.formid from workflow_requestbase r,workflow_base s,workflow_bill l where\n" +
                    " r.workflowid = s.id and s.formid = l.id and  r.requestid =" + requestId + ") s\n" +
                    " where wf_bf.id = wf_si.fieldid and wf_bf.fieldname = '" + fieldname + "'" +
                    " and wf_bf.billid =s.formid and  wf_si.selectvalue ='" + value+"'");
            rs.next();
            return rs.getStringNoTrim("selectname");
        }
        return "";
    }




    //获取主表
    public static RecordSet getPrimaytable (String tablefname,String requestid){
        RecordSet rs = new RecordSet();
        System.out.println("getPrimaytable>>"+"select * from "+tablefname+" where requestid="+requestid+"");
        rs.executeQuery("select * from "+tablefname+" where requestid="+requestid+"");
        return rs;
    }


    //获取明细表1
    public static RecordSet getDetail1(String tablefname,String mainid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from  "+tablefname+"_dt1  where mainid="+mainid+"");
        return rs;
    }

    //获取明细表2
    public static RecordSet getDetail2(String tablefname,String mainid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from  "+tablefname+"_dt2  where mainid="+mainid+"");
        return rs;
    }

    //城市名称
    public static String cityname(String id){
        if (StrUtil.isNotEmpty(id)) {
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select cityname from hrmcity where id =" + id+"");
            rs.next();
            return rs.getStringNoTrim("cityname");
        }
        return "";
    }

    //城市名称
    public static String getWzk(String id){
        if (StrUtil.isNotEmpty(id)) {
            RecordSet rs = new RecordSet();
            System.err.println("select * from uf_yggzktz where ygxm="+id);
            rs.executeQuery(" select * from uf_yggzktz where ygxm=" + id+"");
            rs.next();
            return rs.getStringNoTrim("gzh");
        }
        return "";
    }


    //查询报销单流程数据
    public static Map<String,Object> data(String requestid, String tablefname,String gwccfybxtable){

        //获取主表信息
        RecordSet primarytable = getPrimaytable(tablefname,requestid);

        Map<String,Object> primarytabledata = new HashMap<>();
        RecordSet getStatus = new RecordSet();
        getStatus.execute("select currentnodeid,currentnodetype,status from workflow_requestbase where requestid="+requestid);
        getStatus.next();
        primarytabledata.put("currentnodetype", getStatus.getString("currentnodetype"));
        primarytabledata.put("currentnodeid", getStatus.getString("currentnodeid"));
        primarytabledata.put("currentstatus", getStatus.getString("status"));
        String[] columnName = primarytable.getColumnName();//所有字段名
        boolean bx13 = false;
        //循环读取主表数据
        while (primarytable.next()) {
            for (int k = 0; k < columnName.length; k++) {

                //bx13
                if (columnName[k].equals("bx13")) {
                    bx13 = true;
                    continue;
                }
                //bx27 借款
                if (columnName[k].equals("bx27")) {
                    primarytabledata.put(columnName[k], "bx27");
                    primarytabledata.put("yggzkh", getWzk(primarytable.getStringNoTrim("bxr")));
                    continue;
                }
                //经办人
                if (columnName[k].equals("jbr") || columnName[k].equals("jbry") || columnName[k].equals("xzhj") || columnName[k].equals("bxr") ) {
                    String jbrname = jbrname(primarytable.getStringNoTrim(columnName[k]));
                    primarytabledata.put(columnName[k], jbrname);
                    continue;
                }
                //经费主管科室审批人
                if (columnName[k].equals("zhkspr")) {
                    String jfzgksshr = jfzgksshr(primarytable.getStringNoTrim(columnName[k]));
                    primarytabledata.put(columnName[k], jfzgksshr);
                    continue;
                }

                //部门
                if (columnName[k].equals("jbks") || columnName[k].equals("zbbm") ) {
                    String bmname = bmname(primarytable.getStringNoTrim(columnName[k]));
                    primarytabledata.put(columnName[k], bmname);
                    continue;
                }

                //城市名称转换
                if (columnName[k].equals("tqmdd")){
                    //String cityname = cityname(primarytable.getStringNoTrim(columnName[k])); change by huangwm 2021年12月22日17:40:22 字段类型已经为单位文本
                    String cityname = primarytable.getStringNoTrim(columnName[k]);
                    primarytabledata.put(columnName[k],cityname);
                    continue;
                }

                //单位
                if (columnName[k].equals("jbbm")) {
                    logger.info("单位jbbm："+primarytable.getStringNoTrim(columnName[k]));
                    String dwname = dwname(primarytable.getStringNoTrim(columnName[k]));
                    primarytabledata.put(columnName[k], dwname);
                    continue;
                }

                //数字转换文字
                if (columnName[k].equals("sfdwpc") || columnName[k].equals("sftyaphs")||columnName[k].equals("hylb")
                        || columnName[k].equals("sfcxjk") || columnName[k].equals("hyxgdylx")||columnName[k].equals("csjjtgjlx")
                        || columnName[k].equals("ysly") || columnName[k].equals("zclx") || columnName[k].equals("zfqd")
                        || columnName[k].equals("zcqd") || columnName[k].equals("zzlx") || columnName[k].equals("zffs")
                        || columnName[k].equals("zflx") || columnName[k].equals("sfnrtzgl") || columnName[k].equals("hdlx")
                        || columnName[k].equals("hdxz") || columnName[k].equals("tqlx") || columnName[k].equals("shifxuyyksp")
                        || columnName[k].equals("sfqdht") || columnName[k].equals("ccsy") ) {
                    //2021年8月22日20:54:32 huangwm 增加 columnName[k].equals("ccsy")
                    String getselectname = getselectname(requestid, columnName[k], primarytable.getStringNoTrim(columnName[k]));
                    primarytabledata.put(columnName[k], getselectname);
                    continue;
                }
                primarytabledata.put(columnName[k], primarytable.getStringNoTrim(columnName[k]));
            }
        }

        //流程id
        String id = primarytabledata.get("id").toString();

        logger.info("流程id："+id);

        //计算小计
        if (tablefname.equals(VersionFlowUtil.getFlowTableName(gwccfybxtable))){
            RecordSet rs = new RecordSet();
            rs.executeQuery("select sum(snjtbzts)snjtbzts,sum(zsts)zsts,sum(hsbzbz)hsbzbz from "+tablefname+"_dt1 " +
                    " where mainid="+id);
            rs.next();


            primarytabledata.put("snjtfxj",rs.getStringNoTrim("snjtbzts") );//市内交通费小计
            primarytabledata.put("wcbzfqynxj",rs.getStringNoTrim("zsts"));//误餐补助费(区域内）
            primarytabledata.put("wcbzfqywxj",rs.getStringNoTrim("hsbzbz"));//误餐补助费(区域外）

        }



        //获取明细表1数据
        RecordSet detail1 = getDetail1(tablefname,id);

        List<Map<String,Object>> detail1data = new ArrayList<>();
        String[] detail1columnName = detail1.getColumnName();//所有字段名

        //循环读取明细表1数据
        while (detail1.next()){
            Map<String,Object> map = new HashMap<>();

            for (int k = 0; k < detail1columnName.length; k++){

                //科目代码
                if (detail1columnName[k].equals("xzkmdm")){
                    String kmdm = kmdm(detail1.getStringNoTrim(detail1columnName[k]));
                    map.put(detail1columnName[k],kmdm);
                    continue;
                }


                //经济支出分类
                if ( detail1columnName[k].equals("bmysjjfl") || detail1columnName[k].equals("zfysjjfl") ||
                        detail1columnName[k].equals("zfjjzcfl")|| detail1columnName[k].equals("bmjjzcfl")){
                    String getbmjjczfl = ReimbursementDao.getbmjjczfl(detail1.getStringNoTrim(detail1columnName[k]));
                    map.put(detail1columnName[k],getbmjjczfl);
                    continue;
                }

                //单位
                if ( detail1columnName[k].equals("szssdw") || detail1columnName[k].equals("ffbm")||detail1columnName[k].equals("bxrdw")) {
                    String dwname = dwname(detail1.getStringNoTrim(detail1columnName[k]));
                    map.put(detail1columnName[k], dwname);
                    continue;
                }

                //城市名称转换
                if (detail1columnName[k].equals("hddd")){
                    String cityname = cityname(detail1.getStringNoTrim(detail1columnName[k]));
                    map.put(detail1columnName[k],cityname);
                    continue;
                }


                //预算项目
                if (detail1columnName[k].equals("ysxm")){
                    String projectname = ReimbursementDao.getProjectname(detail1.getStringNoTrim(detail1columnName[k]));
                    map.put(detail1columnName[k], projectname);
                    continue;
                }


                //经办人
                if (detail1columnName[k].equals("bxr")||detail1columnName[k].equals("zqr") ){
                    if(!bx13){
                        String jbrname = jbrname(detail1.getStringNoTrim(detail1columnName[k]));
                        map.put(detail1columnName[k],jbrname);
                    }else{
                        map.put(detail1columnName[k],detail1.getStringNoTrim(detail1columnName[k]));
                    }
                    continue;
                }


                //选择框数字转换文字
                if (detail1columnName[k].equals("zflx")||detail1columnName[k].equals("rylx")||
                        detail1columnName[k].equals("ysly")||detail1columnName[k].equals("zclx")||
                        detail1columnName[k].equals("zfqd") ||   detail1columnName[k].equals("zcqd")||
                        detail1columnName[k].equals("hyxgdylx") || detail1columnName[k].equals("fyysxm")
                        ||detail1columnName[k].equals("cx") || detail1columnName[k].equals("ztsjzcfylx")
                        ||detail1columnName[k].equals("wwsx")||detail1columnName[k].equals("csjjtgjlx")
                        ||detail1columnName[k].equals("sfsyzjgc") ||detail1columnName[k].equals("zhrmb")
                        ||detail1columnName[k].equals("ryzcxm") || detail1columnName[k].equals("ccsy")
                        || detail1columnName[k].equals("zwjb")){
                    //2021年8月22日20:54:32 huangwm 增加 detail1columnName[k].equals("ccsy") || detail1columnName[k].equals("zwjb")
                    String getselectname = getselectname(requestid, detail1columnName[k], detail1.getStringNoTrim(detail1columnName[k]));
                    map.put(detail1columnName[k],getselectname);
                    continue;
                }
                map.put(detail1columnName[k],detail1.getStringNoTrim(detail1columnName[k]));

            }
            logger.info("明细表1数据"+map);
            detail1data.add(map);
        }


        //获取明细表2数据
        RecordSet detail2 = getDetail2(tablefname,id);
        List<Map<String,Object>>  detail2data = new ArrayList<>();
        String[] detail2columnName = detail2.getColumnName();//所有字段名
        //循环读取明细表1数据
        while (detail2.next()){
            Map<String,Object> map = new HashMap<>();

            for (int k = 0; k < detail2columnName.length; k++){

                //数字转换文字
                if (detail2columnName[k].equals("zzlx")||detail2columnName[k].equals("zffs")||
                        detail2columnName[k].equals("zflx")){
                    String getselectname = getselectname(requestid, detail2columnName[k], detail2.getStringNoTrim(detail2columnName[k]));
                    map.put(detail2columnName[k],getselectname);
                    continue;
                }
                map.put(detail2columnName[k],detail2.getStringNoTrim(detail2columnName[k]));
            }
            detail2data.add(map);
        }


        Map<String,Object> Processinformation = new HashMap<>();
        Processinformation.put("primarytabledata",primarytabledata);
        Processinformation.put("detail1list",detail1data);
        Processinformation.put("detail2list",detail2data);


        return  Processinformation;
    }

}
